package com.util.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.util.common.FilePath;

@Scope("singleton")
@Repository("urlProperty")
public class URLProperty {
	
	private final String urlPath = "WEB-INF/classes/properties/url.properties";
	private Properties urlProp;
	
	/**
	 * 获得url日志配置说明。
	 * @return
	 */
	public Properties getUrlProp(){
		if(urlProp == null){
			urlProp = getProp(this.urlPath);
		}
		return urlProp;
	}
	
	public static Properties getProp(String propPath){
		propPath = FilePath.getWEBRoot() + propPath;
		Properties prop = null;
		InputStream in = null;
		try {
			in = new FileInputStream(new File(propPath));
			prop = new Properties();
			prop.load(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			IOUtils.closeQuietly(in);
		}
		return prop;
	}
	
	/**
	 * 刷新url配置信息。
	 */
	public void flushUrlProp(){
		InputStream in = null;
		try {
			in = URLProperty.class.getClassLoader().getResourceAsStream(urlPath);
			urlProp = new Properties();
			urlProp.load(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			IOUtils.closeQuietly(in);
		}
	}
}
